<?php
namespace app\home\controller;
use think\facade\View;
use think\facade\Lang;

/**
 * ============================================================================
 * DSKMS多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Connectwx extends BaseMall
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/connectwx.lang.php');
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/login-register.lang.php');
    }
    /**
     * 微信登录
     */
    public function index(){
        if(empty(input('get.code'))) {
            echo View::fetch($this->template_dir.'index');
        } else {
            $this->get_info();
        }

    }

    /**
     * 回调获取信息
     */
    public function get_info(){
        $code = input('get.code');
        if(!empty($code)) {
            $user_info = $this->get_user_info($code);
            if(!empty($user_info['unionid'])) {
                $unionid = $user_info['unionid'];
                $member_model = model('member');
                $member = $member_model->getMemberInfo(array('member_wxunionid'=> $unionid));
                if(!empty($member)) {//会员信息存在时自动登录
                    if (!$member['member_state']) {//1为启用 0 为禁用
                        $this->error(lang('login_index_account_stop'));
                    }
                    $member_model->createSession($member,'login');
                    //是否有卖家账户
                    $seller_model = model('seller');
                    $seller_info = $seller_model->getSellerInfo(array('member_id' => $member['member_id']));
                    if ($seller_info) {
                        // 更新卖家登陆时间
                        $seller_model->editSeller(array('last_logintime' => TIMESTAMP), array('seller_id' => $seller_info['seller_id']));

                        $sellergroup_model = model('sellergroup');
                        $seller_group_info = $sellergroup_model->getSellergroupInfo(array('sellergroup_id' => $seller_info['sellergroup_id']));

                        $store_model = model('store');
                        $store_info = $store_model->getStoreInfoByID($seller_info['store_id']);

                        $seller_model->createSellerSession($member, $store_info, $seller_info, is_array($seller_group_info) ? $seller_group_info : array());
                    }
                    $this->success(lang('login_index_login_success'),'member/index');
                }
                if(session('member_id')) {//已登录时绑定微信
                    $member_id = session('member_id');
                    $member = array();
                    $member['member_wxunionid'] = $unionid;
                    $member['member_pc_wxopenid'] = $user_info['unionid'];
                    $member['member_wxnickname'] = isset($user_info['nickname']) ? $user_info['nickname'] : get_rand_nickname();
                    $member_model->editMember(array('member_id'=> $member_id),$member,$member_id);
                    $this->success(lang('wechat_binding_was_successful'),'member/index');
                } else {
                        //自动注册会员并登录
                        $logic_connect_api = model('connectapi', 'logic');

                        $reg_info = array(
                            'member_pc_wxopenid'=>$user_info['openid'],, #开发者帐号唯一标识,与公众号标识不同
                            'member_wxunionid'=>$user_info['unionid'],
                            'nickname'=> isset($user_info['nickname'])?$user_info['nickname']:get_rand_nickname(),
                            'headimgurl'=> isset($user_info['headimgurl'])?$user_info['headimgurl']:'',#提高体验暂时不对图片进行处理
                        ); 
                        $wx_member = $logic_connect_api->wx_register($reg_info,'wx');
                        if (!empty($wx_member)) {
                            if (!$wx_member['member_state']) {//1为启用 0 为禁用
                                $this->error(lang('login_index_account_stop'));
                            }
                            $member_model->createSession($wx_member, 'register'); //自动登录
                            $success_message = lang('login_index_login_success');
                            $this->success($success_message, HOME_SITE_URL);
                        }else{
                            $this->error(lang('login_usersave_regist_fail'), 'login/register'); //"会员注册失败"
                        }
                }
            }
        }
        $this->error(lang('wechat_binding_was_failed'),'login/login');
    }
    
    
    /**
     * 获取用户个人信息
     */
    public function get_user_info($code){
        $weixin_pc_appid = config('ds_config.weixin_pc_appid');
        $weixin_pc_secret = config('ds_config.weixin_pc_secret');
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$weixin_pc_appid.'&secret='.$weixin_pc_secret.'&code='.$code.'&grant_type=authorization_code';
        $access_token = http_request($url);//通过code获取access_token
        $code_info = json_decode($access_token, true);
        $user_info = array();
        if(!empty($code_info['access_token'])) {
            $token = $code_info['access_token'];
            $openid = $code_info['openid'];
            $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid;
            $result = http_request($url);//获取用户个人信息
            $user_info = json_decode($result, true);
        }
        return $user_info;
    }
}